<?php


namespace App\Models;


use App\Http\Constants\Account\CommonCoinCoinConstants;
use App\Http\Constants\CreditsAdminOplogConstants;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;

class CreditsAdminOplog extends Model
{
    protected $table = 'life_credits_admin_oplog';

    protected $appends = ['action_type_name','module_type_name'];

    protected $fillable = [
        'id',
        'business_id',
        'user_id',
        'username',
        'mobile',
        'module_type',
        'action_type',
        'ip',
        'old_data',
        'new_data',
        'created_at',
        'updated_at',
    ];

    protected $casts = [
        'username' => 'string',
    ];

    public function getActionTypeNameAttribute()
    {
        $attributes = $this->attributes;
        $value = array_key_exists('action_type', $attributes) ? $attributes['action_type'] : null;
        if (is_null($value)) return null;
        $map = array_column( CreditsAdminOplogConstants::ACTION_TYPE, 'label', 'value');
        return array_key_exists($value, $map) ? $map[$value] : null;
    }

    public function getModuleTypeNameAttribute()
    {
        $attributes = $this->attributes;
        $value = array_key_exists('module_type', $attributes) ? $attributes['module_type'] : null;
        if (is_null($value)) return null;
        $map = array_column( CreditsAdminOplogConstants::MODULES, 'label', 'value');
        return array_key_exists($value, $map) ? $map[$value] : null;
    }

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

}
